package com.cn.why.film.filmindex.controller;

import com.cn.why.film.filmindex.entity.PetProduce;
import com.cn.why.film.filmindex.service.PetMartService;
import com.cn.why.film.filmindex.util.GetIpUtil;
import com.cn.why.film.filmindex.util.PageUtil;
import com.cn.why.film.filmindex.util.Result;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宠物商店控制层
 */
@Controller
@RequestMapping("/petMart")
public class PetMartController {
    @Autowired
    private PetMartService service;

    /**
     * 跳转查询
     * @return
     */
    @RequestMapping("/")
    public String jump(){
        return "redirect:/petMart/daoHangJumpToMark";
    }

    /**
     * 商店信息查询
     */
    @RequestMapping("/jumpToPetMart")
    public String jumpToPetMark(HttpSession session, Model model, String msg1, String msg2){
        //先移除session中的值
        session.removeAttribute("list");
        PageUtil<PetProduce> pageUtil = new PageUtil<>();
        List<PetProduce> list = service.selectProByMsg(msg1,msg2);
        Map<String,Object> map = new HashMap<>();
        //获得map分页后的数据
        map = pageUtil.getPageList(1,list);
        //前台需要的list(分页)
        List<PetProduce> listPetPro = (List<PetProduce>) map.get("list");
        if(listPetPro != null){
            model.addAttribute("listPro",listPetPro);
            //放入model中总页数
            model.addAttribute("countPage",map.get("countPage"));
            //加入session中
            session.setAttribute("list",list);
        }else {
            model.addAttribute("msg","对不起！你搜索的商品不存在！");
        }
        return "petShop/otherHtml/petmart";
    }

    /**
     * 宠物商店
     * @return
     */
    @RequestMapping("/petMartList")
    @ResponseBody
    public Map<String,Object> petMartList(HttpSession session,Integer page){
        Map<String,Object> map = new HashMap<>();
        PageUtil<PetProduce> pageUtil = new PageUtil<>();
        //获得session中的值
        List<PetProduce> list = (List<PetProduce>) session.getAttribute("list");
        //分页查询
        map = pageUtil.getPageList(page,list);
        return map;
    }

    /**
     * 查看商品详情
     */
    @RequestMapping("/jumpProduces")
    public String jumpProduces(Model model,Integer id,HttpSession session){
        //看看session中有没有phoneNumber
        Integer number;
        if(session.getAttribute("phoneNumber") == null){
            number = 0;
        }else {
            number = service.findOneShopCar((int)session.getAttribute("phoneNumber")).size();
        }
        //根据id查询商品
        PetProduce produce = service.findOneById(id);
        model.addAttribute("number",number);
        model.addAttribute("produce",produce);
        return "petShop/pet_produces/pet_produce";
    }

    /**
     * 商品详情页面获取请求的真实ip
     */
    @RequestMapping("/getIp")
    @ResponseBody
    public Map<String,Object> getIp(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        map.put("ip", GetIpUtil.getIp(request));
        return map;
    }

    /**
     * 购物车数量查询
     */
    public Map<String,Object> getCountPro(long phoneNumber){
        Map<String,Object> map = new HashMap<>();
        return map;
    }

    /**
     * 跳转到购物车页面
     */
    @RequestMapping("/jumpToShopCar")
    public  String jumpToShopCar(){
        return "petShop/shop_car/shop_cars";
    }

    public String order(){
        return "";
    }

}
